package com.young.shop.api.service.impl;

import com.young.shop.api.dao.OrderMapper;
import com.young.shop.api.service.OrderService;
import com.young.shop.pojo.entity.Order;
import com.young.shop.pojo.vo.OrderServiceVO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderMapper orderMapper;

    @Override
    public List<Order> mountByUserId(Long userId) {
        List<Order> orders = orderMapper.selectByUserId(userId);
//        if (orderServiceVO == null){
//            throw new RuntimeException("查询订单列表失败");
//        }
        return orders;
    }

    @Override
    public int changStatus(Long userId, Long orderId, int status) {
        int newStatus;
        if (status != 2 & status >=1 & status <= 5){
            newStatus = status + 1;
        }else {
            newStatus = status;
        }
        int i = orderMapper.upDataStatus(userId, orderId, newStatus);
        if (i != 1){
            throw new RuntimeException("申请修改订单状态失败");
        }
        return i;
    }

    @Override
    public List<OrderServiceVO> queryByStatus(Long userId, int status) {
        List<OrderServiceVO> orderServiceVOS = orderMapper.selectByStatus(userId, status);
        if (orderServiceVOS == null){
            throw new RuntimeException("查询失败或许状态订单为空");
        }
        return orderServiceVOS;
    }

    @Override
    public List<OrderServiceVO> particularsByOrderId(Long orderId) {
        List<OrderServiceVO> orderServiceVOS = orderMapper.selectByOrderId(orderId);
        if (orderServiceVOS == null){
            throw new RuntimeException("订单详情查询失败");
        }
        return orderServiceVOS;
    }

    @Override
    public int PaymentOrderByOrderId(String orderId) {
        int i = orderMapper.upDatePaymentOrder(orderId);
        if (i == 0 ){
            throw new RuntimeException("修改订单状态为已支付失败");
        }
        return i;
    }

    @Override
    public int refundOrderByOrderId(String orderId) {
        int i = orderMapper.upDateRefundByOrderId(orderId);
        return i;
    }

    @Override
    public int cancellationOrderByOrderId(Long orderId) {
        int i = orderMapper.upDateCancellationByOrderId(orderId);
        if (i == 0){
            throw new RuntimeException("修改订单状态为申请取消失败");
        }
        return i;
    }

    @Override
    public int applyForRefund(Long orderId) {
        int i = orderMapper.upDateCancellationByOrderId(orderId);
        if (i == 0){
            throw new RuntimeException("修改订单状态值为申请退款失败");
        }
        return i;
    }

    @Override
    public int deleteOrder(Long orderId) {
        int i = orderMapper.upDateConfirmReceipt(orderId);
        if (i == 0){
            throw new RuntimeException("删除订单失败");
        }
        return i;
    }

    @Override
    public int confirmReceipt(Long orderId) {
        int i = orderMapper.upDateDeleteOrder(orderId);
        if (i == 0){
            throw new RuntimeException("待评论订单修改失败");
        }
        return i;
    }


//    @Override
//    public void execute(JobExecutionContext jobExecutionContext) {
//        //检查所有未支付订单列表(根据订单状态为1，查询所有的订单创建时间与订单id）
//        List<OrderServiceVO> orderServiceVOS = orderMapper.selectByStatusOne();
//        System.out.println("=====================");
//        //判断订单是否超时(判断下单时间和当前时间是否超过15分钟，)
//        //过期时间为15分钟
//        int i;
//        Long time = 15 * 60 * 1000L;  //十五分钟
//        Date newDate = new Date(); //当前时间
//        for (OrderServiceVO createData : orderServiceVOS) {
//            Date expirationDate = new Date(createData.getCreateDate().getTime() + time);  //过期时间
//            i = expirationDate.compareTo(newDate);
//            //取消订单
//            if (i<1){
//                //根据订单id修改订单状态值
//                orderMapper.upDateByOrderId(createData.getId());
//            }
//            //释放库存资源
//        }
//    }
}
